unit TestLogLexer;

interface

uses SysUtils, MuTests;

implementation

uses LogLexer;

procedure CheckLexem(const Lexem: string; Column: Integer);
begin
  Check(LogLexer.Value = Lexem, 'LogLexer.Value mismatch, current value is "%s", but "%s" excepted', [LogLexer.Value, Lexem]);
  Check(LogLexer.Column = Column, 'LogLexer.Column mismatch, current value is %d, but %d expected', [LogLexer.Column, Column]);
end;

procedure CheckTakeId(const Id: string; Column: Integer);
begin
  Check(TakeId() = True, 'CheckTakeId() fails: TakeId returns False, but "%s" expected', [Id]);
  CheckLexem(Id, Column);
end;

procedure CheckLookupId(const Id: string; Column: Integer);
begin
  Check(LookupId() = True, 'CheckLookupId() fails: LookupId returns False, but "%s" expected', [Id]);
  CheckLexem(Id, Column);
end;

procedure CheckTakeWord(const Word: string; Column: Integer);
begin
  Check(TakeWord() = True, 'CheckTakeWord() fails: TakeWord returns False, but "%s" expected', [Word]);
  CheckLexem(Word, Column);
end;

procedure CheckLookupWord(const Word: string; Column: Integer);
begin
  Check(LookupWord() = True, 'CheckLookupWord() fails: LookupWord returns False, but "%s" expected', [Word]);
  CheckLexem(Word, Column);
end;

procedure CheckTakeSymbol(const Symbol: string; Column: Integer);
begin
  Check(TakeSymbol() = True, 'CheckTakeSymbol() fails: TakeSymbol returns False, but "%s" expected', [Symbol]);
  CheckLexem(Symbol, Column);
end;

procedure CheckLookupSymbol(const Symbol: string; Column: Integer);
begin
  Check(LookupSymbol() = True, 'CheckLookupSymbol() fails: LookupSymbol returns False, but "%s" expected', [Symbol]);
  CheckLexem(Symbol, Column);
end;

procedure CheckTail(const Tail: string; Column: Integer);
begin
  Check(TakeTail() = True, 'CheckTail() fails: TakeTail returns False, but "%s" expected', [Tail]);
  CheckLexem(Tail, Column);
end;

procedure CheckLookupConst(ConstType: Integer; const ConstValue: string; Column: Integer);
begin
  Check(LookupConst() = ConstType, 'LookupConst() returns unexpected const type. ConstType = %d', [ConstType]);
  CheckLexem(ConstValue, Column);
end;

procedure CheckTakeConst(ConstType: Integer; const ConstValue: string; Column: Integer);
begin
  Check(TakeConst() = ConstType, 'TakeConst() returns unexpected const type. ConstType = %d', [ConstType]);
  CheckLexem(ConstValue, Column);
end;

procedure CheckEol();
begin
  Check(LogLexer.Eol() = True, 'LogLexer.Eol() fails: Eol returns False, but True expected');
end;

procedure Test_01();
begin
  NewLine('# Test comment line 1');
  CheckEol();
  NewLine('  # Test comment line 2');
  CheckEol();
end;

procedure Test_02();
begin
  Reset();
  NewLine('   id1   id2   #');
  CheckLookupId('id1', 4);
  CheckTakeId('id1', 4);
  CheckLookupId('id2', 10);
  CheckTakeId('id2', 10);
  Check(LookupId() = False, 'LookupId() returns True, but there are no identifiers in a line');
  Check(TakeId() = False, 'TakeId() returns True, but there are no identifiers in a line');
  CheckEol();
end;

procedure Test_03();
begin
  Reset();
  NewLine('    Test_Long_Identifier_With_Underline_666 12 ');
  CheckLookupId('Test_Long_Identifier_With_Underline_666', 5);
  CheckTakeId('Test_Long_Identifier_With_Underline_666', 5);
  Check(LookupId() = False, 'LookupId() returns True, but there are no identifiers in a line');
  Check(TakeId() = False, 'TakeId() returns True, but there are no identifiers in a line');
  CheckLookupWord('12', 45);
  CheckTakeWord('12', 45);
  CheckEol();
end;

procedure Test_04();
begin
  Reset();
  NewLine('OPTION FOR C++ IdentifierStyle = Camel # This is demonsttaion for OPTION clause');

  CheckLookupId('OPTION', 1);
  CheckLookupWord('OPTION', 1);
  CheckTakeWord('OPTION', 1);

  CheckLookupId('FOR', 8);
  CheckLookupWord('FOR', 8);
  CheckTakeWord('FOR', 8);

  CheckLookupId('C', 12);
  CheckLookupWord('C++', 12);
  CheckTakeWord('C++', 12);

  CheckLookupId('IdentifierStyle', 16);
  CheckLookupWord('IdentifierStyle', 16);
  CheckTakeWord('IdentifierStyle', 16);

  Check(TakeId() = False, 'TakeId() returns True, but there are no identifiers in a line');
  CheckLookupSymbol('=', 32);
  CheckTakeSymbol('=', 32);

  CheckLookupId('Camel', 34);
  CheckLookupWord('Camel', 34);
  CheckTail('Camel', 34);

  Check(LookupWord() = False, 'LookupWord() returns True, but there are no identifiers in a line');
  Check(LookupId() = False, 'LookupId() returns True, but there are no identifiers in a line');
  Check(TakeWord() = False, 'TakeWord() returns True, but there are no identifiers in a line');
  Check(TakeId() = False, 'TakeId() returns True, but there are no identifiers in a line');
  Check(TakeSymbol() = False, 'TakeId() returns True, but there are no identifiers in a line');
  CheckEol();
end;

procedure Test_05();
begin
  Reset();
  NewLine(' 1024  12 # This is demonsttaion for OPTION clause');

  Check(LookupId() = False, 'LookupId() returns True, but there are no identifiers in a line');
  CheckLookupWord('1024', 2);
  CheckLookupConst(CONST_INTEGER, '1024', 2);
  CheckTakeConst(CONST_INTEGER, '1024', 2);

  Check(LookupId() = False, 'LookupId() returns True, but there are no identifiers in a line');
  CheckLookupWord('12', 8);
  CheckLookupConst(CONST_INTEGER, '12', 8);
  CheckTakeConst(CONST_INTEGER, '12', 8);

  Check(LookupWord() = False, 'LookupWord() returns True, but there are no identifiers in a line');
  Check(LookupId() = False, 'LookupId() returns True, but there are no identifiers in a line');
  Check(LookupSymbol() = False, 'LookupSymbol() returns True, but there are no identifiers in a line');
  Check(TakeWord() = False, 'TakeWord() returns True, but there are no identifiers in a line');
  Check(TakeId() = False, 'TakeId() returns True, but there are no identifiers in a line');
  Check(TakeSymbol() = False, 'TakeSymbol() returns True, but there are no identifiers in a line');
  CheckEol();
end;



const
  Tests: array[1..6] of TTest = (
    (Name: 'Test_01'; Proc: Test_01;),
    (Name: 'Test_02'; Proc: Test_02;),
    (Name: 'Test_03'; Proc: Test_03;),
    (Name: 'Test_04'; Proc: Test_04;),
    (Name: 'Test_05'; Proc: Test_05;),
    (Name: ''; Proc: nil)
  );

initialization
  RegisterTests('LogLexer', Tests);


end.